package com.riche.codefactory.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.riche.codefactory.common.BaseResponse;
import com.riche.codefactory.common.ErrorCode;
import com.riche.codefactory.common.R;
import com.riche.codefactory.exception.BusinessException;
import com.riche.codefactory.exception.ThrowUtils;
import com.riche.codefactory.model.c2s.post.C2sPostQueryRequest;
import com.riche.codefactory.model.c2s.postfavour.C2sPostFavourAddRequest;
import com.riche.codefactory.model.c2s.postfavour.C2sPostFavourQueryRequest;
import com.riche.codefactory.model.domain.Post;
import com.riche.codefactory.model.domain.User;
import com.riche.codefactory.model.s2c.S2cPost;
import com.riche.codefactory.service.IPostFavourSVC;
import com.riche.codefactory.service.IPostSVC;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import com.riche.codefactory.service.IUserSVC;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 帖子收藏接口
 *
 *

 */
@RestController
@RequestMapping("/post_favour")
@Slf4j
public class PostFavourController {

    @Resource
    private IPostFavourSVC IPostFavourSVC;

    @Resource
    private IPostSVC IPostSVC;

    @Resource
    private IUserSVC userService;

    /**
     * 收藏 / 取消收藏
     *
     * @param c2sPostFavourAddRequest
     * @param request
     * @return resultNum 收藏变化数
     */
    @PostMapping("/")
    public BaseResponse<Integer> doPostFavour(@RequestBody C2sPostFavourAddRequest c2sPostFavourAddRequest,
            HttpServletRequest request) {
        if (c2sPostFavourAddRequest == null || c2sPostFavourAddRequest.getPostId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        // 登录才能操作
        final User loginUser = userService.getLoginUser(request);
        long postId = c2sPostFavourAddRequest.getPostId();
        int result = IPostFavourSVC.doPostFavour(postId, loginUser);
        return R.ok(result);
    }

    /**
     * 获取我收藏的帖子列表
     *
     * @param c2sPostQueryRequest
     * @param request
     */
    @PostMapping("/my/list/page")
    public BaseResponse<Page<S2cPost>> listMyFavourPostByPage(@RequestBody C2sPostQueryRequest c2sPostQueryRequest,
                                                              HttpServletRequest request) {
        if (c2sPostQueryRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        User loginUser = userService.getLoginUser(request);
        long current = c2sPostQueryRequest.getCurrent();
        long size = c2sPostQueryRequest.getPageSize();
        // 限制爬虫
        ThrowUtils.throwIf(size > 20, ErrorCode.PARAMS_ERROR);
        Page<Post> postPage = IPostFavourSVC.listFavourPostByPage(new Page<>(current, size),
                IPostSVC.getQueryWrapper(c2sPostQueryRequest), loginUser.getId());
        return R.ok(IPostSVC.getPostVOPage(postPage, request));
    }

    /**
     * 获取用户收藏的帖子列表
     *
     * @param c2sPostFavourQueryRequest
     * @param request
     */
    @PostMapping("/list/page")
    public BaseResponse<Page<S2cPost>> listFavourPostByPage(@RequestBody C2sPostFavourQueryRequest c2sPostFavourQueryRequest,
                                                            HttpServletRequest request) {
        if (c2sPostFavourQueryRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        long current = c2sPostFavourQueryRequest.getCurrent();
        long size = c2sPostFavourQueryRequest.getPageSize();
        Long userId = c2sPostFavourQueryRequest.getUserId();
        // 限制爬虫
        ThrowUtils.throwIf(size > 20 || userId == null, ErrorCode.PARAMS_ERROR);
        Page<Post> postPage = IPostFavourSVC.listFavourPostByPage(new Page<>(current, size),
                IPostSVC.getQueryWrapper(c2sPostFavourQueryRequest.getC2sPostQueryRequest()), userId);
        return R.ok(IPostSVC.getPostVOPage(postPage, request));
    }
}
